<?php
include_once('common/header.php');			
if($AppUI->id <=0 || !isset($_SERVER['HTTP_REFERER']) || (isset($_SERVER['HTTP_REFERER'])&&!Vfriend_Function::isValidLink($_SERVER['HTTP_REFERER']))) {
	Vfriend_Function::redirect(HOST_VFRIEND);
}
$intUserId 			= Vfriend_Function::base64D($objRequest->getParam('puserid', 0));
$strRefer 			= $objRequest->getParam('prefer', LINK_PLACE);
$intPlaceId 		= $objRequest->getParam('pplaceid', 0);
$intCategoryId		= $objRequest->getParam('pcategoryid', 0);
$intSubCategoryId	= $objRequest->getParam('psubcategoryid', 0);
$strName			= $objRequest->getParam('pname', '');
$strCountry			= 'VN';
$intProvince		= $objRequest->getParam('pprovince', 0);
$intDistrict		= $objRequest->getParam('pdistrict', 0);
$strAddress			= $objRequest->getParam('paddress', '');

$oFile				= $_FILES['pimage'];
$strContent			= $objRequest->getParam('pcontent','');
$strMap				= $objRequest->getParam('pmap', '');
$strImageOld		= $objRequest->getParam('pimage_old', '');

$strImage			= '';
$intError			= 0;
if($intUserId != $AppUI->id) $intError = 13;
//elseif($intCategoryId == 0) $intError = 9;
elseif($intSubCategoryId == 0) $intError = 10;
elseif($strName == '') $intError = 1;
elseif($strName != '' && strlen($strName) < 5) $intError = 2;
elseif($intProvince == 0) $intError = 4;
elseif($intDistrict == 0) $intError = 12;
elseif($strAddress == '') $intError = 13;
elseif($strAddress != '' && strlen($strAddress) < 5) $intError = 15;
elseif(!isset($oFile['name']) && $strImageOld == '') $intError = 11;
// elseif(strip_tags($strContent) == '') $intError = 5;
// elseif(strip_tags($strContent) != '' && strlen(strip_tags($strContent))<20) $intError = 6;

if($intError == 0 && isset($oFile['name']) && $oFile['name'] != '') {
	$arrImage = Vfriend_Upload::uploadPlaceImg($oFile, $strImage);	
	if($arrImage['error'] > 0) {
		$intError = 3;
	}
}
if($intError == 0) {
	if(isset($_SESSION['sPlaceError'])) unset($_SESSION['sPlaceError']);
	if(isset($_SESSION['sPlaceEdit'])) unset($_SESSION['sPlaceEdit']);
	$objPlace	= new Place();	
	$intError	= $objPlace->update($AppUI->id, $intPlaceId, $strName, $intCategoryId, $intSubCategoryId, $strCountry, $intProvince, $intDistrict, $strAddress, $strContent, $strMap, $strImage);
	if($intError == 0) {
		Vfriend_Function::setMsg($arrMessage[2]);
		Vfriend_Function::redirect($strRefer);
	} else {
		$intError = 8;
	}
}
if($intError > 0) {
	$objPlace	= new Place();
	$row		= $objPlace->get($intPlaceId);
	$_SESSION['sPlaceError']	= $arrError[$intError];
	$_SESSION['sPlaceEdit']		= $objRequest->getParams();
	Vfriend_Function::redirect(Vfriend_Function::linkEditPlaces($intPlaceId, $row['name']));
}
?>
